package com.xmb.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmb.common.constants.RankTypeEnum;
import com.xmb.common.dao.XmbRankConfigMapper;
import com.xmb.common.entity.XmbConstantMessage;
import com.xmb.common.entity.XmbRankConfig;
import com.xmb.common.entity.reward.ActivityRanking;
import com.xmb.common.entity.reward.RankConfig;
import com.xmb.common.exception.BusinessException;
import com.xmb.common.interceptor.CurrentContextUser;
import com.xmb.common.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyndon on 2019/6/17.
 */
@Service
public class RankConfigServiceImpl implements RankConfigService{

    @Resource
    private XmbRankConfigMapper rankConfigMapper;

    @Resource
    private UserBalanceService balanceService;

    @Resource
    private UserScoreService scoreService;

    @Resource
    private UserBonusService bonusService;

    @Resource
    private XmbConstantMessageService constantMessageService;

    @Override
    public int saveRankConfig(XmbRankConfig rankConfig) {
        return rankConfigMapper.insertSelective(rankConfig);
    }

    @Override
    public int updateRankConfig(XmbRankConfig rankConfig) {
        return rankConfigMapper.updateByPrimaryKeySelective(rankConfig);
    }

    @Override
    public List<XmbRankConfig> findRankConfig(Long senceId, String senceType) {
        XmbRankConfig rankConfig = new XmbRankConfig();
        rankConfig.setActivityId(senceId);
        rankConfig.setModule(senceType);
        return rankConfigMapper.findRankConfigBySenceIdAndType(rankConfig);
    }

    @Override
    public int deleteXmbOrderRewardSetting(Long id) {
        return rankConfigMapper.deleteXmbOrderRewardSetting(id);
    }

    @Override
    public List<RankConfig> getActivityRewardConfigList(Long activityId) {
        List<RankConfig> configList = new ArrayList<>();
        List<XmbRankConfig> rankConfigList = rankConfigMapper.findRankConfigByActivityId(activityId);
        rankConfigList.forEach(rankConfig -> {
            XmbConstantMessage message
                    = constantMessageService.findXmbConstantMessage("marketing_reward_rank"
                    .concat("_")
                    .concat(rankConfig.getModule()),rankConfig.getOrderFiled());
            if (message != null) {
                configList.add(JSONObject.parseObject(String.format(message.getBusinessValue(),rankConfig.getId()),RankConfig.class));
            }
        });
        return configList;
    }

    @Override
    public PageInfo getActivityRewardRanking(Long id,int pageNum,int pageSize) {

        PageInfo pageInfo = null;
        XmbRankConfig rankConfig = rankConfigMapper.selectByPrimaryKey(id);
        if (rankConfig == null) {
            throw new BusinessException("没有找到当前活动的排行榜的配置信息");
        }
        List<ActivityRanking> rankingList = new ArrayList<>();
        // 余额
        if (RankTypeEnum.balance.name().equals(rankConfig.getOrderFiled())) {
            pageInfo = PageHelper.startPage(pageNum,pageSize).doSelectPageInfo(()
                    -> balanceService.getBalanceRanking(rankConfig.getActivityId()));
        }
        // 积分
        if (RankTypeEnum.score.name().equals(rankConfig.getOrderFiled())) {
            pageInfo = PageHelper.startPage(pageNum,pageSize).doSelectPageInfo(()
                    -> scoreService.getScoreRanking(rankConfig.getActivityId()));
        }
        // 奖金
        if (RankTypeEnum.bonus.name().equals(rankConfig.getOrderFiled())) {
            pageInfo = PageHelper.startPage(pageNum,pageSize).doSelectPageInfo(()
                    -> bonusService.getBonusRanking(rankConfig.getActivityId()));
        }
        return pageInfo;
    }

    @Override
    public ActivityRanking getCurrentUserActivityRewardRanking(Long id) {
        ActivityRanking activityRanking = null;
        XmbRankConfig rankConfig = rankConfigMapper.selectByPrimaryKey(id);
        if (rankConfig == null) {
            throw new BusinessException("没有找到当前活动的排行榜的配置信息");
        }
        // 余额
        if (RankTypeEnum.balance.name().equals(rankConfig.getOrderFiled())) {
            activityRanking = balanceService.getCurrentUserActivityRewardRanking(rankConfig.getActivityId(),CurrentContextUser.get().getUserId());
        }
        // 积分
        if (RankTypeEnum.score.name().equals(rankConfig.getOrderFiled())) {
            activityRanking = scoreService.getCurrentUserActivityRewardRanking(rankConfig.getActivityId(),CurrentContextUser.get().getUserId());
        }
        // 奖金
        if (RankTypeEnum.bonus.name().equals(rankConfig.getOrderFiled())) {
            activityRanking = bonusService.getCurrentUserActivityRewardRanking(rankConfig.getActivityId(), CurrentContextUser.get().getUserId());
        }
        return activityRanking;
    }

}
